package demo2;


public class Test {
    //2.声明异常
    public static void main1(String[] args) throws CloneNotSupportedException{
        Person person1 = new Person("zhangsan",10,85.12);
        System.out.println("person1:" + person1);
        //3.强制类型转换，此时是向下转型
        Person person2 = (Person) person1.clone();
        System.out.println("person2:" + person2);

        System.out.println(person1 == person2); //输出false
        //克隆后：内容一样 person1:demo1.Person{name='zhangsan', age=10, score=85.12}
        //              person2:demo1.Person{name='zhangsan', age=10, score=85.12}
        //       地址不一样  person1:demo2.Person@682a0b20
        //                 person1:demo2.Person@682a0b20
        //表明 person1和person2指向了两个不同的对象
    }

    public static void main2(String[] args) throws CloneNotSupportedException{
        Person person1 = new Person("zhangsan",10,85.12);
        Person person2 = (Person) person1.clone();
        System.out.println(person1.m.money); //9.9
        System.out.println(person2.m.money); //9.9
        person2.m.money = 19.9;
        System.out.println(person1.m.money); //19.9
        System.out.println(person2.m.money); //19.9
        //由此说明，通过clone，我们只是拷贝了Person对象。但是Person对象中的Money对象，并没有拷贝。
        // 通过person2这个引用修改了m的值后，person1这个引用访问m的时候，值也发生了改变。
        // person1.m和person2.m 指向了同一个money。这里就是发生了浅拷贝。
    }

    //深拷贝
    public static void main(String[] args) throws CloneNotSupportedException{
        Person person1 = new Person("zhangsan",10,85.12);
        Person person2 = (Person) person1.clone();
        System.out.println(person1.m.money); //9.9
        System.out.println(person2.m.money); //9.9
        person2.m.money = 19.9;
        System.out.println(person1.m.money); //9.9
        System.out.println(person2.m.money); //19.9

    }
}
